package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


import entity.Type;
import util.db.DBhelp;
import util.db.Page;
import util.db.RowMapper;

public class TypeDao {

	DBhelp<Type> dBhelp = new DBhelp<Type>();
	
	public Type findtypeByTypename(String typename) {
		String sql = "select id,typename,createtime from t_type where typename=?";
		return dBhelp.executeQueryToObject(sql, new typeRowMapper(), typename);
	}
	
	public void saveType(String id,String typename) {
		System.out.println(id+"      "+typename);
		String sql = "INSERT into t_type (id,typename) values(?,?)";
		dBhelp.update(sql, id,typename);
	}


	private class typeRowMapper implements RowMapper<Type>{

		public Type mapRow(ResultSet resultSet) throws SQLException {
			Type type = new Type();
			type.setId(resultSet.getString("id"));
			type.setTypename(resultSet.getString("typename"));
			type.setCreatetime(resultSet.getString("createtime"));
			return type;
		}
		
	}


	public Page<Type> findAllType(int nowpagenum) {
		Page<Type> page = new Page<Type>(nowpagenum, findCount());
		String sql = "select id,typename,createtime from t_type order by createtime desc limit ?,?";
		List<Type> list = dBhelp.executeQueryForList(sql, new typeRowMapper(), page.getStartPage(),page.getPagesize());
		
		page.setList(list);
		return page;
	}
	
	public int findCount(){
		String sql = "select count(*) from t_type";
		return dBhelp.executeQuery(sql).intValue();
	}

	public Type findTypeById(String id) {
		String sql = "select id,typename,createtime from t_type where id=?";
		return dBhelp.executeQueryToObject(sql, new typeRowMapper(), id);
		
	}

	public void update(Type type) {
		String sql = "update t_type set typename=? where id=?";
		dBhelp.update(sql, type.getTypename(),type.getId());
	}

	public void delTypeById(String id) {
		String sql = "delete from t_type where id=?";
		dBhelp.update(sql, id);
	}

	public List<Type> findAll() {
		String sql = "select * from t_type";
		return dBhelp.executeQueryForList(sql, new typeRowMapper());
	}

	/*public Page<Todo> fineAllTodo(int uid,int nowPageNum){
		Page<Todo> page = new Page<Todo>(nowPageNum,fineCount(uid) );
		String sql = "SELECT id,todo,state,createtime,uid FROM todo where uid=? order by createtime desc limit ?,?";
		List<Todo> list = dBhelp.executeQueryForList(sql, new TodoRowMapper(),uid,page.getStartPage(),page.getPagesize());
		page.setList(list);
		return page;
	}
	
	public int fineCount(int uid){
		String sql = "SELECT count(*) FROM todo where uid=?";
		return dBhelp.executeQuery(sql,uid).intValue();
	}*/
	

	
}
